package com.zlc.workflow.core.business.service.impl;

import com.zlc.common.core.business.model.vo.StoneReimbursementProcessVo;
import com.zlc.workflow.anno.WorkflowCallback;
import com.zlc.workflow.anno.WorkflowCallbackFunction;
import com.zlc.workflow.constant.EventTimeEnum;
import com.zlc.workflow.core.business.service.ReimbursementService;
import com.zlc.workflow.core.instance.model.EventParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

@Slf4j
@WorkflowCallback(value = "reimbursementCallback", description = "报销回调", module = "workflow")
public class ReimbursementCallback implements WorkflowCallbackFunction {

    @Autowired
    private ReimbursementService reimbursementService;

    @Override
    public void callback(EventParam context) {
        StoneReimbursementProcessVo reimburse = reimbursementService.findByProcessInstanceId(context.getProcessInstanceId());
        if(ObjectUtils.isEmpty(reimburse)){
            reimburse = new StoneReimbursementProcessVo();
            reimburse.setProcessInstanceId(context.getProcessInstanceId());
            reimburse.setApplyUser(context.getActorId());
            reimburse.setApplyUserName(context.getActorName());
            reimburse.setApplyStatus("DRAFT");
        }
        String eventTime = context.getEventTime();
        if (EventTimeEnum.EVENT_PROCESS_END.getType().equals(eventTime)) {
            //流程结束
            reimburse.setApplyStatus("SUCCESS");
        } else if (EventTimeEnum.EVENT_PROCESS_START.getType().equals(eventTime)) {
            //流程启动
            reimburse.setApplyStatus("DRAFT");
        } else if (EventTimeEnum.EVENT_PROCESS_TERMINATION.getType().equals(eventTime)) {
            //流程终止
            reimburse.setApplyStatus("TERMINATION");
        } else if(EventTimeEnum.EVENT_TASK_END.getType().equals(eventTime) && context.isFirstTask()){
            reimburse.setApplyStatus("PROCESSING");
        }
        reimbursementService.saveReimbursement(reimburse);
    }
}
